﻿using System;
using System.Windows.Forms;
using ReportWizard.DataAccess;
using ReportWizard.Helpers;
using ReportWizard.Properties;

namespace ReportWizard.Forms
{
    public partial class EditUserInfoForm : IconForm
    {
        private UserInfo CurrentUser { get; set; }

        public EditUserInfoForm(int userId)
        {
            InitializeComponent();
            SetupForm();

            CurrentUser = UserInfo.Select(userId);
        }

        public EditUserInfoForm()
        {
            InitializeComponent();
            SetupForm();
        }

        private void SetupForm()
        {
            ShowCustomRightButton = false;
            ShowCheckFooter = false;
        }

        private void FillUserInfo()
        {
            if (CurrentUser != null)
            {
                OkButtonText = Resources.Save;
                tbUserLogin.Enabled = false;

                tbUserFirstName.Text = CurrentUser.FirstName;
                tbUserLastName.Text = CurrentUser.LastName;
                tbUserLogin.Text = CurrentUser.Login;
                checkUserAdmin.Checked = CurrentUser.Admin;
                checkUserActive.Checked = CurrentUser.Active;

                HeaderText = string.Format("Данные пользователя {0}", CurrentUser);
            }
            else
            {
                checkUserActive.Checked = true;
            }
        }

        private void ClearUserFields()
        {
            for (int i = 0; i < tlpUserInfoLayout.Controls.Count; i++)
            {
                Control fieldControl = tlpUserInfoLayout.Controls[i];
                if (fieldControl != null && fieldControl.Enabled)
                {
                    if (fieldControl.GetType().Equals(typeof(TextBox)))
                    {
                        ((TextBox)fieldControl).Clear();
                    }
                    else if (fieldControl.GetType().Equals(typeof(CheckBox)))
                    {
                        ((CheckBox)fieldControl).Checked = false;
                    }
                }
            }
        }

        private void CancelClick(object sender, EventArgs e)
        {
            Close();
        }

        private void AddUserFormLoad(object sender, EventArgs e)
        {
            FillUserInfo();
        }

        private void ClearUserInfoClick(object sender, EventArgs e)
        {
            ClearUserFields();
        }

        private void UpdateCurrentUser()
        {
            CurrentUser.Update();
            if (CurrentUser.Id.Equals(Globals.CurrentUser.Id))
            {
                Globals.CurrentUser = CurrentUser;
            }
            DialogResult = DialogResult.OK;
            Close();
        }

        private void SaveUserInfoClick(object sender, EventArgs e)
        {
            string firstName = tbUserFirstName.Text.Trim();
            string lastName = tbUserLastName.Text.Trim();
            string login = tbUserLogin.Text.Trim();

            if (CurrentUser == null)
                CurrentUser = new UserInfo();

            if (!string.IsNullOrEmpty(firstName) && !string.IsNullOrEmpty(lastName) && !string.IsNullOrEmpty(login))
            
            {
                CurrentUser.FirstName = firstName;
                CurrentUser.LastName = lastName;
                CurrentUser.Admin = checkUserAdmin.Checked;
                CurrentUser.Active = checkUserActive.Checked;
                

                string userPass1 = tbUserPassword.Text;
                string userPass2 = tbUserRepeatPassword.Text;

                if (CurrentUser.Id > 0)
                {
                    if (!string.IsNullOrEmpty(userPass1) && !string.IsNullOrEmpty(userPass2))
                    {
                        if (userPass1.Equals(userPass2))
                        {
                            CurrentUser.Password = userPass1;
                            UpdateCurrentUser();
                        }
                        else
                        {
                            ShowMessage.Error(Resources.PasswordIncorrectRepeated);
                        }
                    }
                    else
                    {
                        UpdateCurrentUser();
                    }
                }
                else
                {
                    UserInfo ui = UserInfo.Select(login);
                    if (ui == null || ui.Deleted)
                    {

                        CurrentUser = new UserInfo
                                          {
                                              FirstName = firstName,
                                              LastName = lastName,
                                              Login = login,
                                              Active = checkUserActive.Checked,
                                              Admin = checkUserAdmin.Checked
                                          };

                        if (!string.IsNullOrEmpty(userPass1) && !string.IsNullOrEmpty(userPass2))
                        {
                            if (userPass1.Equals(userPass2))
                            {
                                CurrentUser.Password = userPass1;
                                CurrentUser.Insert();
                                DialogResult = DialogResult.OK;
                                Close();
                            }
                            else
                            {
                                ShowMessage.Error(Resources.PasswordIncorrectRepeated);
                            }
                        }
                        else
                        {
                            ShowMessage.Error(Resources.TypeAndRepeatPassword);
                        }
                    }
                    else
                    {
                        ShowMessage.Error(Resources.LoginIsNotFree);
                    }
                }
            }
            else
            {
                ShowMessage.Error(Resources.FillAllFields);
            }
        }
    }
}